package com.ruoyi.system.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.JiajiaOrder;
import com.ruoyi.system.domain.JiajiaOrderSettleDetail;
import com.ruoyi.system.domain.JiajiaWorkerSettleScale;
import com.ruoyi.system.domain.vo.IncreaseSumStatisticsVo;
import com.ruoyi.system.mapper.JiajiaOrderMapper;
import com.ruoyi.system.mapper.JiajiaOrderSettleDetailMapper;
import com.ruoyi.system.mapper.JiajiaWorkerSettleScaleMapper;
import com.ruoyi.system.service.IJiajiaOrderService;
import com.ruoyi.system.service.IJiajiaWorkerService;
import com.ruoyi.system.task.dto.OrderSettleTaskDto;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 订单Service业务层处理
 *
 * @author knd
 * @date 2021-10-14
 */
@Service
public class JiajiaOrderServiceImpl implements IJiajiaOrderService
{
    @Autowired
    private JiajiaOrderMapper jiajiaOrderMapper;

    @Resource
    private JiajiaWorkerSettleScaleMapper jiajiaWorkerSettleScaleMapper;

    @Resource
    private JiajiaOrderSettleDetailMapper jiajiaOrderSettleDetailMapper;

    @Autowired
    private IJiajiaWorkerService jiajiaWorkerService;

    /**
     * 查询订单
     *
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public JiajiaOrder selectJiajiaOrderById(Long id)
    {
        return jiajiaOrderMapper.selectJiajiaOrderById(id);
    }

    /**
     * 查询订单列表
     *
     * @param jiajiaOrder 订单
     * @return 订单
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<JiajiaOrder> selectJiajiaOrderList(JiajiaOrder jiajiaOrder)
    {
        return jiajiaOrderMapper.selectJiajiaOrderList(jiajiaOrder);
    }

    /**
     * 新增订单
     *
     * @param jiajiaOrder 订单
     * @return 结果
     */
    @Override
    public int insertJiajiaOrder(JiajiaOrder jiajiaOrder)
    {
        return jiajiaOrderMapper.insertJiajiaOrder(jiajiaOrder);
    }

    /**
     * 修改订单
     *
     * @param jiajiaOrder 订单
     * @return 结果
     */
    @Override
    public int updateJiajiaOrder(JiajiaOrder jiajiaOrder)
    {
        return jiajiaOrderMapper.updateJiajiaOrder(jiajiaOrder);
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Override
    public int deleteJiajiaOrderByIds(String ids)
    {
        return jiajiaOrderMapper.deleteJiajiaOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除订单信息
     *
     * @param id 订单主键
     * @return 结果
     */
    @Override
    public int deleteJiajiaOrderById(Long id)
    {
        return jiajiaOrderMapper.deleteJiajiaOrderById(id);
    }

    @Override
    public void settleAccounts(Date startTime, Date endTime) {
        //查找技师的时间范围内技师id及订单总金额
        List<OrderSettleTaskDto> wTotalAmount =jiajiaOrderMapper.selectTotalAmount(startTime,endTime);
        //技师结算阶梯比例查找
        List<JiajiaWorkerSettleScale> jiajiaWorkerSettleScales=jiajiaWorkerSettleScaleMapper.workerSettleScaleList();
        //添加结算记录

        if (ObjectUtils.isEmpty(wTotalAmount)) {

        }else {

            for (OrderSettleTaskDto taskDto : wTotalAmount) {
                JiajiaOrderSettleDetail jiajiaOrderSettleDetail=new JiajiaOrderSettleDetail();
                jiajiaOrderSettleDetail.setTotalAmount(taskDto.getTotalAmount());
                jiajiaOrderSettleDetail.setWid(taskDto.getwId());
                jiajiaOrderSettleDetail.setStartTime(startTime);
                jiajiaOrderSettleDetail.setEndTime(endTime);
                BigDecimal multiply;
                //遍历阶梯比例查找出该技师结算比例并计算结算金额后赋值
                for (JiajiaWorkerSettleScale scale :jiajiaWorkerSettleScales) {
                    if ((taskDto.getTotalAmount().compareTo(scale.getCondition()))>0){
                        multiply = scale.getRate().multiply(taskDto.getTotalAmount()).divide(new BigDecimal(100));
                        jiajiaOrderSettleDetail.setPayAmount(multiply);
                        jiajiaOrderSettleDetail.setRate(scale.getRate());
                        break;
                    }
                }
                jiajiaOrderSettleDetailMapper.insertJiajiaOrderSettleDetail(jiajiaOrderSettleDetail);
                //查找该技师时间范围内的所有订单
                List<JiajiaOrder> jiajiaOrder=jiajiaOrderMapper.select(taskDto.getwId(),startTime,endTime);
                for (JiajiaOrder order:jiajiaOrder) {
                    //给该技师时间范围内相应订单插入订单结算id
                    order.setOsdid(jiajiaOrderSettleDetail.getId());
                    jiajiaOrderMapper.updateJiajiaOrder(order);
                }
            }
        }
    }

    @Override
    public IncreaseSumStatisticsVo increaseSumStatistics(IncreaseSumStatisticsVo increaseSumStatisticsVo ) {


        return increaseSumStatisticsVo;
    }

    @Override
    public JiajiaOrder selectorderList(Long id) {
        JiajiaOrder jiajiaOrder =jiajiaOrderMapper.selectorderList(id);
        return jiajiaOrder;
    }

    @Override
    public Long monthOrderNum(JiajiaOrder jiajiaOrder) {
        return jiajiaOrderMapper.monthOrderNum(jiajiaOrder);
    }

    @Override
    @DataScope(deptAlias = "d")
    public BigDecimal totalIncome(JiajiaOrder jiajiaOrder) {
        return jiajiaOrderMapper.totalIncome( jiajiaOrder);
    }

    @Override
    @DataScope(deptAlias = "d")
    public BigDecimal yearIncome(JiajiaOrder jiajiaOrder) {
        return jiajiaOrderMapper.yearIncome(jiajiaOrder);
    }

    @Override
    @DataScope(deptAlias = "d")
    public BigDecimal quarterIncome(JiajiaOrder jiajiaOrder) {
        return jiajiaOrderMapper.quarterIncome(jiajiaOrder);
    }

    @Override
    @DataScope(deptAlias = "d")
    public BigDecimal weekIncome(JiajiaOrder jiajiaOrder) {
        return jiajiaOrderMapper.weekIncome(jiajiaOrder);
    }

    @Override
    @DataScope(deptAlias = "d")
    public Long totalOrderNum(JiajiaOrder jiajiaOrder) {
        return jiajiaOrderMapper.totalOrderNum(jiajiaOrder);
    }

    @Override
    @DataScope(deptAlias = "d")
    public Long yearOrderNum(JiajiaOrder jiajiaOrder) {
        return jiajiaOrderMapper.yearOrderNum(jiajiaOrder);
    }

    @Override
    @DataScope(deptAlias = "d")
    public Long quarterOrderNum(JiajiaOrder jiajiaOrder) {
        return jiajiaOrderMapper.quarterOrderNum(jiajiaOrder);
    }

    @Override
    @DataScope(deptAlias = "d")
    public Long weekOrderNum(JiajiaOrder jiajiaOrder) {
        return jiajiaOrderMapper.yearOrderNum(jiajiaOrder);
    }

    @Override
    @DataScope(deptAlias = "d")
    public BigDecimal monthIncome(JiajiaOrder jiajiaOrder) {
        return jiajiaOrderMapper.monthIncome(jiajiaOrder);
    }

    @Override
    public Long selectMonthOrderNum(JiajiaOrder jiajiaOrder) {
        return jiajiaOrderMapper.selectMonthOrderNum(jiajiaOrder);
    }

    @Override
    public BigDecimal selectMonthIncome(JiajiaOrder jiajiaOrder) {
        return jiajiaOrderMapper.selectMonthIncome(jiajiaOrder);
    }

    @Override
    public List<JiajiaOrder> selectJiajiaOrderByList(JiajiaOrder order) {
        return jiajiaOrderMapper.selectJiajiaOrderByList(order);
    }

    @Override
    public void quartzTask() {
        //取出社工点击完成的数据
        JiajiaOrder jiajiaOrder=new JiajiaOrder();
        jiajiaOrder.setStatus(6);
        List<JiajiaOrder> jiajiaOrders = jiajiaOrderMapper.selectJiajiaOrderList(jiajiaOrder);

        jiajiaOrders.stream().filter(jiajiaOrder1 -> DateUtils.addDays(jiajiaOrder1.getEndTime(),7).before(new Date())  )
                            .forEach( jiajiaOrder2 ->{
                                jiajiaWorkerService.apiOrderStatus(jiajiaOrder2,9);
                            });
    }


}
